package com.douguo.recipe.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;

import com.douguo.recipe.CalendarActivity;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Calendar;
import com.douguo.recipe.object.EkArticle;
import com.douguo.recipe.object.HomeRecipe;

public class GetEkArticleList {
	public static void GetHotEkArticleList(String sortId, int index, int number, final Handler handler) {
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		NameValuePair nvp = new BasicNameValuePair("cate_id", sortId);
		NameValuePair offset = new BasicNameValuePair("offset", String.valueOf(index));
		NameValuePair num = new BasicNameValuePair("num", String.valueOf(number));
		nvps.add(nvp);
		nvps.add(offset);
		nvps.add(num);
		new Thread(new Runnable() {

			public void run() {
				DouguoConnection mConn = new DouguoConnection(
						Constant.URL_GET_EK_ARTICLE_THUMBLIST, nvps, mParseListener,
						DouguoConnection.GET_METHOD);
				List<EkArticle> calendars = (List<EkArticle>) mConn.Connect();
				handler.sendMessage(Message.obtain(handler,
						Constant.GET_EK_ARTICLES, calendars));
			}
		}).start();
	}
	
	private static OnParseListener mParseListener = new OnParseListener() {

		public Object onParsing(InputStream is) {
			List<EkArticle> articles = null;
			XmlPullParser parser = Xml.newPullParser();
			EkArticle currentArticle = null;
			boolean done = false;

			try {

				parser.setInput(is, null);
				int eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT && !done) {
					String name = null;
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						articles = new ArrayList<EkArticle>();
						break;
					case XmlPullParser.START_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("blog")) {
							currentArticle = new EkArticle();
							currentArticle.id = parser.getAttributeValue(null, "id");
							
						} else if (currentArticle != null) {
							if (name.equalsIgnoreCase("id")) {
								currentArticle.id = parser.nextText();
							} else if (name.equalsIgnoreCase("excerpt")) {
								currentArticle.excerpt = parser.nextText();
							} else if (name.equalsIgnoreCase("title")) {
								currentArticle.title = parser.nextText();
							} else if (name.equalsIgnoreCase("publish_time")) {
								currentArticle.createtime = parser.nextText();
							} else  if(name.equalsIgnoreCase("thumb")) {
								currentArticle.thumb = parser.nextText();
							}

						}
						break;
					case XmlPullParser.END_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("blog")
								&& currentArticle != null) {
//							Log.d(TAG, currentHomeRecipe.getBanner());
							articles.add(currentArticle);
						}
						break;
					case XmlPullParser.END_DOCUMENT:
						done = true;
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return articles;
		}

	};
}
